#coding:utf-8
from django.http import HttpResponse,HttpResponseRedirect,Http404
from django.shortcuts import render
from models import dnspod_info
from django.contrib.auth.decorators import login_required
from forms import AddForm
import json



@login_required(login_url="/login/")
def dnspod_view(request):
    if request.method == 'POST':# 当提交表单时
        form_info = AddForm(request.POST) # form 包含提交的数据
        if form_info.is_valid():# 如果提交的数据合法
            #form = form_info.cleaned_data
            form_info.save()
            return HttpResponseRedirect('/dnspod')
        #else:
    else:# 当正常访问时
        form_info = AddForm()
    #加载界面的时候请求一次
    dnspod_list = dnspod_info.objects.all()
    return render(request, 'dnspod_view.html',locals())


@login_required(login_url="/login/")
def dnspod_del(request,dnspod_id):
    try:
        num = int(dnspod_id)
    except ValueError:
        raise Http404
    node = dnspod_info.objects.get(id=num)
    node.delete()
    return HttpResponseRedirect('/dnspod')